local extension = Package("rfenghou_wu")
extension.extensionName = "aaa_fenghou"

local U = require "packages/utility/utility"
local RUtil = require "packages/aaa_fenghou/utility/rfenghou_util"
local DIY = require "packages/diy_utility/diy_utility"

Fk:loadTranslationTable{
  ["rfenghou_wu"] = "鎏吴",
  ["rfuyao"] = "扶摇",
}

local function AddWinAudio(general)
  local Win = fk.CreateActiveSkill{ name = general.name.."_win_audio" }
  Win.package = extension
  Fk:addSkill(Win)
end

--FY008 朱异 by扬林
local zhuyi = General:new(extension, "rfuyao__zhuyi", "wu", 4)
local ruyuan = fk.CreateTriggerSkill{
  name = "rfenghou__ruyuan",
  anim_type = "offensive",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target.phase == Player.Finish and player:getMark("rfenghou__ruyuan-round") == 0 and
      not target.dead then
      local n = 0
      if table.every(player.room:getOtherPlayers(target), function (p)
        return p:getHandcardNum() ~= target:getHandcardNum()
      end) then
        n = n + 1
      end
      if table.every(player.room:getOtherPlayers(target), function (p)
        return p.hp ~= target.hp
      end) then
        n = n + 1
      end
      if n > 0 then
        if player == target then n = 2 end
        self.cost_data = {n = n, tos = {target.id}}
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__ruyuan"..self.cost_data.n.."-invoke::"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data.n == 2 then
      local _,dat = room:askForUseActiveSkill(player, "rfenghou__ruyuan_active", "#rfenghou__ruyuan-ironchain", true)
      if dat then
        if #dat.targets > 0 then
          room:useVirtualCard("iron_chain", nil, player, table.map(dat.targets, Util.Id2PlayerMapper), self.name)
        else
          room:broadcastPlaySound("./audio/system/recast")
          player:drawCards(1, "recast")
        end
      end
    end
    room:damage{
      from = player,
      to = target,
      damage = 1,
      damageType = fk.FireDamage,
      skillName = self.name,
    }
  end,
}

local rfenghou__ruyuan_active = fk.CreateActiveSkill{
  name = "rfenghou__ruyuan_active",
  card_filter = Util.FalseFunc,
  interaction = function()
    return UI.ComboBox { choices = {"method_use", "recast"} }
  end,
  target_filter = function(self, to_select, selected)
    if self.interaction.data == "recast" then return false end
    local card = Fk:cloneCard("iron_chain")
    card.skillName = ruyuan.name
    local to = Fk:currentRoom():getPlayerById(to_select)
    return #selected < card.skill:getMaxTargetNum(Self, card) and Self:canUseTo(card, to)
  end,
  feasible = function(self, selected, selected_cards)
    return #selected > 0 or self.interaction.data == "recast"
  end,
}
Fk:addSkill(rfenghou__ruyuan_active)

local huayan = fk.CreateTriggerSkill{
  name = "rfenghou__huayan",
  anim_type = "offensive",
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.damageType == fk.FireDamage and not target.dead and not player:isNude() and
      player:getMark("@@rfenghou_readying:::"..self.name) == 0 and data.from == player and
      #player.room.logic:getActualDamageEvents(2, function (e)
        local damage = e.data[1]
        return damage.damageType == fk.FireDamage and damage.from == player and damage.to == target
      end, Player.HistoryGame, 1) == 1
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__huayan-invoke::"..target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, {target.id})
    data.damage = data.damage + 3
    room:setPlayerMark(player, "@rfenghou__huayan", 4)
    player:throwAllCards("he")
  end,
}
local huayan_delay = fk.CreateTriggerSkill{
  name = "#rfenghou__huayan_delay",
  mute = true,
  events = {"fk.RfenghouSkillReady", fk.RoundEnd},
  can_trigger = function(self, event, target, player, data)
    if event == "fk.RfenghouSkillReady" then
      return target == player and data == huayan
    else
      return player:getMark("@rfenghou__huayan") > 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == "fk.RfenghouSkillReady" then
      room:setPlayerMark(player, "@rfenghou__huayan", 0)
    else
      room:removePlayerMark(player, "@rfenghou__huayan", 1)
      if player:getMark("@rfenghou__huayan") == 0 then
        room:killPlayer({who = player.id})
      end
    end
  end,
}
huayan.RfenghouReadySkill = true
huayan:addRelatedSkill(huayan_delay)
zhuyi:addSkill(ruyuan)
zhuyi:addSkill(huayan)
AddWinAudio(zhuyi)
Fk:loadTranslationTable{
  ["rfuyao__zhuyi"] = "朱异",
  ["#rfuyao__zhuyi"] = "梁断弦櫜",
  ["illustrator:rfuyao__zhuyi"] = "哥达耀",
  ["designer:rfuyao__zhuyi"] = "扬林",
  ["cv:rfuyao__zhuyi"] = "超绝天",

  ["rfenghou__ruyuan"] = "如渊",
  [":rfenghou__ruyuan"] = "一名角色的结束阶段，若其体力值或手牌数为场上唯一值，你可对其造成一点火焰伤害；若两项均为唯一值或其为你，你可先视为使用或重铸一张【铁索连环】。",
  ["#rfenghou__ruyuan1-invoke"] = "如渊：你可以对 %dest 造成1点火焰伤害",
  ["#rfenghou__ruyuan2-invoke"] = "如渊：你可以对 %dest 造成1点火焰伤害，且此前可视为使用【铁索】",
  ["rfenghou__ruyuan_active"] = "如渊",
  ["#rfenghou__ruyuan-ironchain"] = "如渊：你可视为使用或重铸【铁索连环】",

  ["rfenghou__huayan"] = "化焱",
  [":rfenghou__huayan"] = "<a href='rfenghou_ready_skill'>蓄势技</a>，当你对一名角色第二次造成火焰伤害时，你可以弃置所有牌，令此伤害+3，"..
  "若如此做，此后算起的第四轮结束时，若你未重置此技能，你阵亡。",
  ["#rfenghou__huayan-invoke"] = "化焱：你可以弃置所有牌，令 %dest 受到的伤害+3！",
  ["@rfenghou__huayan"] = "化焱",
  ["@@rfenghou_readying:::rfenghou__huayan"] = "化焱失效",

  ["$rfenghou__ruyuan1"] = "此心战战，如临深而履薄。",
  ["$rfenghou__ruyuan2"] = "上下无常，涉道浅、没足深。",
  ["$rfenghou__huayan1"] = "南岳干、钟山铜，应机方获隼。",
  ["$rfenghou__huayan2"] = "丙丁火、藏乎兑，此兆一线生。",
  ["~rfuyao__zhuyi"] = "功名难望、林木无枝。",
  ["$rfuyao__zhuyi_win_audio"] = "岂有蛟龙愁失水？更无鹰隼与高秋。",
}



--HY001 孙弘 by 廷玉
local sunhong = General:new(extension, "rfenghou__sunhong", "wu", 4)
local lvedong = fk.CreateTriggerSkill{
  name = "rfenghou__lvedong",
  anim_type = "special",
  events = {fk.DrawNCards, fk.EventPhaseProceeding},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      local n = 0
      if event == fk.DrawNCards then
        player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
          for _, move in ipairs(e.data) do
            if move.from == player.id and move.moveReason == fk.ReasonDiscard and move.proposer == player.id then
              n = n + #move.moveInfo
            end
          end
          if n > 0 then
            return true
          end
        end, Player.HistoryGame)
      elseif event == fk.EventPhaseProceeding and player.phase == Player.Discard and not player:isNude() then
        player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
          for _, move in ipairs(e.data) do
            if move.from == player.id and move.moveReason == fk.ReasonDiscard then
              n = n + #move.moveInfo
            end
          end
          if n > 0 then
            return true
          end
        end, Player.HistoryGame)
      end
      if n > 0 then
        self.cost_data = 2 * n
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.DrawNCards then
      local choices = {}
      for i = 0, self.cost_data, 1 do
        table.insert(choices, tostring(i))
      end
      table.insert(choices, "Cancel")
      local choice = player.room:askForChoice(player, choices, self.name, "#rfenghou__lvedong-draw")
      if choice ~= "Cancel" then
        self.cost_data = {choice = choice}
        return true
      end
    elseif event == fk.EventPhaseProceeding then
      local cards = player.room:askForDiscard(player, self.cost_data, 999, true, self.name, true, nil,
        "#rfenghou__lvedong-discard:::"..self.cost_data, true)
      if #cards > 0 then
        self.cost_data = {cards = cards}
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.DrawNCards then
      data.n = tonumber(self.cost_data.choice)
    elseif event == fk.EventPhaseProceeding then
      player._phase_end = true
      player.room:throwCard(self.cost_data.cards, self.name, player, player)
    end
  end,
}
local jiao = fk.CreateTriggerSkill{
  name = "rfenghou__jiao",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and not player:isKongcheng() then
      for _, move in ipairs(data) do
        if move.from == player.id and (move.extra_data or {}).rfenghou__jiao1 then
          return true
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__jiao-draw")
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(2, self.name)
  end,

  refresh_events = {fk.AfterCardsMove},
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(self, true) and (player:getMark("jiao_lose-round") == 0 or player:getMark("jiao_get-round") == 0)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.from == player.id then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            if player:getMark("jiao_lose-round") == 0 then
              room:setPlayerMark(player, "jiao_lose-round", 1)
              move.extra_data = move.extra_data or {}
              move.extra_data.rfenghou__jiao1 = true
            end
          end
        end
      end
      if move.to == player.id and move.toArea == Card.PlayerHand then
        if player:getMark("jiao_get-round") == 0 then
          room:setPlayerMark(player, "jiao_get-round", 1)
          move.extra_data = move.extra_data or {}
          move.extra_data.rfenghou__jiao2 = true
        end
      end
    end
  end,
}
local jiao2 = fk.CreateTriggerSkill{
  name = "#rfenghou__jiao",
  anim_type = "negative",
  main_skill = jiao,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(jiao) and player:getHandcardNum() > 1 then
      for _, move in ipairs(data) do
        if move.to == player.id and (move.extra_data or {}).rfenghou__jiao2 then
          return true
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player.room:askForDiscard(player, 2, 2, true, "rfenghou__jiao", true, nil, "#rfenghou__jiao-discard", true)
    if #cards == 2 then
      self.cost_data = {cards = cards}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:throwCard(self.cost_data.cards, "rfenghou__jiao", player, player)
  end,
}
jiao:addRelatedSkill(jiao2)
sunhong:addSkill(lvedong)
sunhong:addSkill(jiao)
Fk:loadTranslationTable{
  ["rfenghou__sunhong"] = "孙弘",
  ["#rfenghou__sunhong"] = "休休之鸠",
  ["illustrator:rfenghou__sunhong"] = "",
  ["designer:rfenghou__sunhong"] = "廷玉",

  ["rfenghou__lvedong"] = "略懂",
  [":rfenghou__lvedong"] = "摸牌阶段，你可以改为摸至多你上次弃置牌数两倍的牌；弃牌阶段，你可以改为弃置至少你上次摸牌数两倍的牌。",
  ["rfenghou__jiao"] = "极傲",
  [":rfenghou__jiao"] = "当你失去牌后，若你本轮未获得过牌，你可以摸两张牌；当你获得牌后，若你本轮未失去过牌，你可以弃置两张牌。",
  ["#rfenghou__lvedong-draw"] = "略懂：现在是摸牌阶段，是否要更改摸牌数？",
  ["#rfenghou__lvedong-discard"] = "略懂：现在是弃牌阶段，是否要改为弃置至少%arg张牌？",
  ["#rfenghou__jiao-draw"] = "极傲：是否摸两张牌？",
  ["#rfenghou__jiao"] = "极傲",
  ["#rfenghou__jiao-discard"] = "极傲：是否弃置两张牌？",

  ["$rfenghou__lvedong1"] = "据图谋不轨，今奉诏索命。",
  ["$rfenghou__lvedong2"] = "休妄论芍陂之战，当诛之。",
  ["$rfenghou__jiao1"] = "宦海如薄冰，求生逐富贵。",
  ["$rfenghou__jiao2"] = "吾不欲为鱼肉，故为刀俎。",
  ["~rfenghou__sunhong"] = "孙弘",
}

local weiwenzhugezhi = General:new(extension, "rfenghou__weiwenzhugezhi", "wu", 4)

local rfenghou__mingchao = fk.CreateActiveSkill{
  name = "rfenghou__mingchao",
  anim_type = "drawcard",
  prompt = function (self)
    return "#rfenghou__mingchao:::"..(Self:usedSkillTimes(self.name) + 1)
  end,
  target_num = 0,
  interaction = function()
    return UI.ComboBox { choices = {"rfenghou__mingchao1", "rfenghou__mingchao2"} }
  end,
  card_filter = function(self, to_select, selected)
    if not table.contains(Self.player_cards[Player.Hand], to_select) then return false end
    if table.contains(DIY.GetShownCards(Self), to_select) then return false end
    if #selected == (Self:usedSkillTimes(self.name) + 1) then return false end
    if self.interaction.data == "rfenghou__mingchao1" then
      return true
    else
      return not Self:prohibitDiscard(Fk:getCardById(to_select))
    end
  end,
  can_use = function(self, player)
    return not player:isKongcheng()
  end,
  feasible = function(self, selected, selected_cards)
    return #selected_cards == Self:usedSkillTimes(self.name) + 1
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = table.simpleClone(effect.cards)
    if self.interaction.data == "rfenghou__mingchao1" then
      DIY.ShowCards(player, cards)
      if not player.dead then
        player:drawCards(1, self.name)
      end
    else
      room:throwCard(cards, self.name, player, player)
      while not player.dead do
        cards = table.filter(cards, function(id)
          return room:getCardArea(id) == Card.DiscardPile and
          (Fk:getCardById(id).trueName == "slash" or Fk:getCardById(id).type == Card.TypeEquip)
        end)
        if #cards == 0 then break end
        local use = U.askForUseRealCard(room, player, cards, ".", self.name, "#rfenghou__mingchao-use", {expand_pile = cards}, true)
        if use then
          table.removeOne(cards, use.card.id)
          room:useCard(use)
        else
          break
        end
      end
    end
  end,
}
weiwenzhugezhi:addSkill(rfenghou__mingchao)

Fk:loadTranslationTable{
  ["rfenghou__weiwenzhugezhi"] = "卫温诸葛直",
  ["#rfenghou__weiwenzhugezhi"] = "谜络长洲",
  ["designer:rfenghou__weiwenzhugezhi"] = "祭祀",

  ["rfenghou__mingchao"] = "鸣潮",
  [":rfenghou__mingchao"] = "出牌阶段，你可以选一项：1.明置X张暗置牌并摸一张牌；2.弃置X张暗置牌并使用其中的【杀】与装备牌。（X为本回合此技能发动次数+1）",
  ["rfenghou__mingchao1"] = "明置牌并摸一张牌",
  ["rfenghou__mingchao2"] = "弃暗置牌并使用其中杀与装备",
  ["@rfenghou__mingchao"] = "鸣潮",
  ["#rfenghou__mingchao"] = "鸣潮：你可以明置或弃置 %arg 张暗置牌",
  ["#rfenghou__mingchao-use"] = "鸣潮：你可使用其中的【杀】与装备牌",

  ["$rfenghou__mingchao1"] = "宦海沉浮，生死难料！",
  ["$rfenghou__mingchao2"] = "跨海南征，波涛起浮。",
  ["~rfenghou__weiwenzhugezhi"] = "吾皆海岱清士，岂料生死易逝。",
}

local sunluban = General:new(extension, "rfenghou__sunluban", "wu", 3, 3, General.Female)

local rfenghou__luanzheng = fk.CreateTriggerSkill{
  name = "rfenghou__luanzheng",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if player.phase == Player.Start then
      return table.find(player.room.alive_players, function (p)
        return #p:getCardIds("e") > 0
      end)
    elseif player.phase == Player.Finish then
      return player:getMark("rfenghou__luanzheng-turn") ~= 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if player.phase == Player.Finish then return true end
    local tos = room:askForChooseToMoveCardInBoard(player, "#rfenghou__luanzheng-choose:::1", self.name, true, "e", true)
    if #tos > 1 then
      self.cost_data = tos
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if player.phase == Player.Start then
      local excludeIds = {}
      for i = 1, 3 do
        local tos
        if i == 1 then
          tos = table.simpleClone(self.cost_data)
        else
          tos = room:askForChooseToMoveCardInBoard(player, "#rfenghou__luanzheng-choose:::"..i, self.name, true, "e", true, excludeIds)
          if #tos ~= 2 then break end
        end
        tos = table.map(tos, Util.Id2PlayerMapper)
        local move = room:askForMoveCardInBoard(player, tos[1], tos[2], self.name, "e", nil, excludeIds)
        if player.dead or not move then return end
        table.insert(excludeIds, move.card.id)
        room:addTableMark(player, "rfenghou__luanzheng-turn", {move.from, move.card.id})
      end
    else
      local mark = player:getTableMark("rfenghou__luanzheng-turn")
      local num = #mark
      for _, info in ipairs(mark) do
        local to = room:getPlayerById(info[1])
        local cid = info[2]
        local card = Fk:getCardById(cid)
        if not to.dead and room:getCardArea(cid) == Card.PlayerEquip then
          local targets = table.filter(room.alive_players, function(p) return p:canMoveCardIntoEquip(cid, false) end)
          if #targets > 0 then
            local tos = player.room:askForChoosePlayers(to, table.map(targets, Util.IdMapper), 1, 1,
            "#rfenghou__luanzheng-replace:::"..Fk:getCardById(cid):toLogString(), self.name, true)
            if #tos > 0 then
              room:moveCardIntoEquip(room:getPlayerById(tos[1]), cid, self.name, false, to)
            end
          end
        end
        if table.contains(to:getCardIds("e"), cid) then
          num = num - 1
        end
      end
      if num > 0 then
        player:drawCards(num, self.name)
      end
    end
  end,
}
sunluban:addSkill(rfenghou__luanzheng)

local rfenghou__shejin = fk.CreateTriggerSkill{
  name = "rfenghou__shejin",
  events = {fk.HpChanged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player
    and table.find(player.room.alive_players, function (p) return not p:isNude() end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p) return not p:isNude() end)
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#rfenghou__shejin-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    if player.dead or to:isNude() then return end
    local cards = room:askForCardsChosen(player, to, 1, 3, "he", self.name)
    room:throwCard(cards, self.name, to, player)
    if not to.dead and room:askForSkillInvoke(to, self.name, nil, "#rfenghou__shejin-draw:::"..#cards) then
      room:setPlayerMark(to, "@@rfenghou__shejin-turn", 1)
      cards = table.filter(to:drawCards(#cards, self.name), function(id) return table.contains(to:getCardIds("h"), id) end)
      if #cards > 0 and not to.dead then
        DIY.ShowCards(to, cards)
      end
    end
  end,
}

local rfenghou__shejin_prohibit = fk.CreateProhibitSkill{
  name = "#rfenghou__shejin_prohibit",
  prohibit_use = function(self, player, card)
    if player:getMark("@@rfenghou__shejin-turn") > 0 then
      local cards = Card:getIdList(card)
      return table.find(cards, function(id) return not table.contains(DIY.GetShownCards(player), id) end)
    end
  end,
}
rfenghou__shejin:addRelatedSkill(rfenghou__shejin_prohibit)

sunluban:addSkill(rfenghou__shejin)

Fk:loadTranslationTable{
  ["rfenghou__sunluban"] = "孙鲁班",
  ["#rfenghou__sunluban"] = "",
  ["designer:rfenghou__sunluban"] = "",

  ["rfenghou__luanzheng"] = "乱政",
  [":rfenghou__luanzheng"] = "准备阶段，你可以移动场上至多三张装备牌，结束阶段，所有角色依次可以移动其因此失去的牌，最后每有一张牌未归原位，你摸一张牌。",
  ["#rfenghou__luanzheng-choose"] = "乱政：你可以移动场上装备牌（第 %arg 个，至多 3 个）",
  ["#rfenghou__luanzheng-replace"] = "乱政：你曾失去%arg，选择一名角色，将之移动至其装备区！",

  ["rfenghou__shejin"] = "涉矜",
  [":rfenghou__shejin"] = "你的体力值变化后，你可以弃置一名角色至多三张牌，其可以摸等量张牌并明置，令其本回合不能使用暗置牌。",
  ["#rfenghou__shejin-choose"] = "涉矜：你可以弃置一名角色至多三张牌",
  ["@@rfenghou__shejin-turn"] = "涉矜中",
  ["#rfenghou__shejin-draw"] = "涉矜：你可以摸 %arg 张牌并明置，且本回合仅能使用明置牌",

  ["$rfenghou__luanzheng1"] = "我的好妹妹，来世莫要生于帝王家。",
  ["$rfenghou__luanzheng2"] = "至尊有诏，不从长公主者，斩！",
  ["$rfenghou__shejin1"] = "本宫比之猛虎，谁更使人惧乎？",
  ["$rfenghou__shejin2"] = "猛虎食人心肝，本宫灭人神魂。",
  ["~rfenghou__sunluban"]= "生于朱楼高阙，谈何亲情？",
}

local sunxiu = General:new(extension, "rfenghou__sunxiu", "wu", 3)
local yuling = fk.CreateTriggerSkill{
  name = "rfenghou__yuling",
  anim_type = "control",
  events = {fk.RoundEnd},
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(self) then
      local room = player.room
      local targets = {}
      room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        local use = e.data[1]
        if use.card.trueName == "slash" then
          table.insert(targets, use.from)
          return true
        end
      end, Player.HistoryRound)
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId).trueName == "slash" then
                table.insert(targets, move.from)
                return true
              end
            end
          end
        end
      end, Player.HistoryRound)
      room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        local use = e.data[1]
        if use.card.trueName == "slash" then
          table.insert(targets, use.from)
          return true
        end
      end, room.logic:getCurrentEvent().id)
      targets = table.filter(targets, function (id)
        return not room:getPlayerById(id).dead
      end)
      if #targets > 0 then
        room:sortPlayersByAction(targets)
        self.cost_data = {tos = targets}
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    for _, pid in ipairs(self.cost_data.tos) do
      room:addPlayerMark(room:getPlayerById(pid), "@rfenghou__yuling")
    end
    local invoke = room:askForSkillInvoke(player, self.name, nil, "#rfenghou__yuling-invoke")
    for _, pid in ipairs(self.cost_data.tos) do
      room:setPlayerMark(room:getPlayerById(pid), "@rfenghou__yuling", 0)
    end
    return invoke
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = table.map(self.cost_data.tos, Util.Id2PlayerMapper)
    local me = table.contains(tos, player) and player
    for _, to in ipairs(tos) do
      if not to.dead and not to:isNude() then
        if me and not me.dead and me ~= to then
          local cid = room:askForCardChosen(me, to, "he", self.name)
          room:askForYiji(me, {cid}, room:getOtherPlayers(to), self.name, 1, 1, "#rfenghou__yuling-card:"..to.id, {cid})
        else
          room:askForYiji(to, to:getCardIds("he"), room:getOtherPlayers(to), self.name, 1, 1, "#rfenghou__yuling-card:"..to.id)
        end
      end
    end
  end,
}
local yizhu = fk.CreateActiveSkill{
  name = "rfenghou__yizhu",
  mute = true,
  card_num = 0,
  target_num = 1,
  prompt = function (self)
    return "#rfenghou__yizhu"..RUtil.getSwitchState(Self, self.name)
  end,
  can_use = function(self, player)
    return player:getMark("@@rfenghou_readying:::"..self.name) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    if #selected == 0 then
      if RUtil.getSwitchState(Self, self.name) == 1 then
        return not Fk:currentRoom():getPlayerById(to_select).chained
      else
        return true
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:notifySkillInvoked(player, self.name, "control")
    if RUtil.getSwitchState(player, self.name) == 1 then
      player:broadcastSkillInvoke(self.name, table.random({1, 2}))
      RUtil.changeSwitchState(player, self.name)
      room:addTableMark(player, self.name, target.id)
      target:setChainState(true)
    elseif RUtil.getSwitchState(player, self.name) == 2 then
      player:broadcastSkillInvoke(self.name, table.random({3, 4}))
      RUtil.changeSwitchState(player, self.name)
      target:turnOver()
    elseif RUtil.getSwitchState(player, self.name) == 3 then
      player:broadcastSkillInvoke(self.name, table.random({5, 6}))
      RUtil.changeSwitchState(player, self.name)
      room:killPlayer{who = target.id, damage = {to = target, damage = 1, from = player}} -- 虚构damage非常危险
      ---FIXME: deathStruct应该拥有killer参数，取代damage.from判断凶手
    end
  end,

  on_acquire = function (self, player, is_start)
    RUtil.setSwitchState(player, self.name, 1, 3)
  end,
  on_lose = function (self, player, is_death)
    RUtil.removeSwitchSkill(player, self.name)
  end,
}
yizhu.RfenghouReadySkill = true
local yizhu_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__yizhu_trigger",
  mute = true,
  events = {fk.ChainStateChanged},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(yizhu, true) and not target.chained and table.contains(player:getTableMark("rfenghou__yizhu"), target.id)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, "rfenghou__yizhu", "special")
    player:broadcastSkillInvoke("rfenghou__yizhu", table.random({7, 8}))
    RUtil.setSwitchState(player, self.name, 1, 3)
    room:setPlayerMark(player, "@@rfenghou_readying:::rfenghou__yizhu", 0)
    room.logic:trigger("fk.RfenghouSkillReady", player, yizhu, false)
  end,
}
local yuxing = fk.CreateTriggerSkill{
  name = "rfenghou__yuxing$",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Start and
      player:canUse(Fk:cloneCard("await_exhausted"))
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#rfenghou__yuxing-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = Fk:cloneCard("await_exhausted")
    card.skillName = self.name
    local use = {
      from = player.id,
      card = card,
    }
    use.extra_data = use.extra_data or {}
    use.extra_data.rfenghou__yuxing = player.id
    room:useCard(use)
    if player.dead then return end
    local cards = player:getTableMark("rfenghou__yuxing-tmp")
    room:setPlayerMark(player, "rfenghou__yuxing-tmp", 0)
    cards = table.filter(cards, function (id)
      return table.contains(room.discard_pile, id)
    end)
    if #cards < 2 then return end
    cards = room:askForGuanxing(player, cards, {2, 2}, nil, self.name, true, {"Top", "pile_discard"}).top
    if #cards == 2 then
      room:moveCards({
        ids = table.reverse(cards),
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        skillName = self.name,
        proposer = player.id,
      })
    end
  end,

  refresh_events = {fk.AfterCardsMove},
  can_refresh = function(self, event, target, player, data)
    if not player.dead then
      local cards = {}
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard and move.skillName == "await_exhausted_skill" then
          local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
          if use_event ~= nil then
            local use = use_event.data[1]
            if (use.extra_data or {}).rfenghou__yuxing == player.id then
              for _, info in ipairs(move.moveInfo) do
                if table.contains(player.room.discard_pile, info.cardId) then
                  table.insertIfNeed(cards, info.cardId)
                end
              end
            end
          end
        end
      end
      if #cards > 0 then
        cards = U.moveCardsHoldingAreaCheck(player.room, cards)
        if #cards > 0 then
          self.cost_data = cards
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local mark = player:getTableMark("rfenghou__yuxing-tmp")
    table.insertTableIfNeed(mark, self.cost_data)
    player.room:setPlayerMark(player, "rfenghou__yuxing-tmp", mark)
  end,
}
yizhu:addRelatedSkill(yizhu_trigger)
sunxiu:addSkill(yuling)
sunxiu:addSkill(yizhu)
sunxiu:addSkill(yuxing)
Fk:loadTranslationTable{
  ["rfenghou__sunxiu"] = "孙休",
  ["#rfenghou__sunxiu"] = "孤翼不折",
  ["illustrator:rfenghou__sunxiu"] = "兴游",
  ["designer:rfenghou__sunxiu"] = "zzcclll朱苦力",
  ["cv:rfenghou__sunxiu"] = "zzcclll朱苦力",

  ["rfenghou__yuling"] = "御凌",
  [":rfenghou__yuling"] = "每轮结束时，你可以令本轮最先使用【杀】、最先弃置【杀】、最后使用【杀】的角色依次分配一张牌。若含你，均由你分配。",
  ["rfenghou__yizhu"] = "抑诛",
  [":rfenghou__yizhu"] = "转换技，<a href='rfenghou_ready_skill'>蓄势技</a>，出牌阶段，你可以令一名角色①横置②翻面③死亡。"..
  "当你发动①选择的角色武将牌重置后，重置此技能的转换技选项和蓄势技状态。",
  ["rfenghou__yuxing"] = "聿兴",
  [":rfenghou__yuxing"] = "主公技，准备阶段，你可以视为使用【以逸待劳】，结算后将因此弃置的两张牌置于牌堆顶。",
  ["#rfenghou__yuling-invoke"] = "御凌：是否令满足条件的角色各分配一张牌？",
  ["#rfenghou__yuling-card"] = "御凌：请分配 %src 的一张牌",
  ["@rfenghou__yuling"] = "御凌分配",
  ["#rfenghou__yizhu1"] = "抑诛：你可以令一名角色横置！",
  ["#rfenghou__yizhu2"] = "抑诛：你可以令一名角色翻面！",
  ["#rfenghou__yizhu3"] = "抑诛：你可以杀死一名角色！",
  ["@@rfenghou_readying:::rfenghou__yizhu"] = "抑诛失效",
  ["#rfenghou__yizhu_trigger"] = "抑诛",
  ["#rfenghou__yuxing-invoke"] = "聿兴：是否视为使用【以逸待劳】？",

  ["$rfenghou__yuling1"] = "御国之术者，政出于一。",
  ["$rfenghou__yuling2"] = "天命有常者，在予一人。",
  ["$rfenghou__yizhu1"] = "彼招摇过市，其心可知也。",
  ["$rfenghou__yizhu2"] = "彼跋扈至此，令孤芒刺在背。",
  ["$rfenghou__yizhu3"] = "庙堂甚高，无卿之禄。",
  ["$rfenghou__yizhu4"] = "金樽共汝饮，公自求多福吧。",
  ["$rfenghou__yizhu5"] = "白刃不相饶！",
  ["$rfenghou__yizhu6"] = "诛此獠！使乱臣贼子惧。",
  ["$rfenghou__yizhu7"] = "尔来非亮剑之时也。",
  ["$rfenghou__yizhu8"] = "诛枭立威，非之时也，且姑待之。",
  ["$rfenghou__yuxing1"] = "群贤皆蜂至，良策自涌来！",
  ["$rfenghou__yuxing2"] = "闻良臣之言，如闻丝竹之音，足可慰也！",
  ["~rfenghou__sunxiu"] = "社稷无常奉，君臣无常位……",
}

local lie = General:new(extension, "rfenghou__lie", "wu", 3, 3, General.Female)

local rfenghou__mingqi = fk.CreateActiveSkill{
  name = "rfenghou__mingqi",
  anim_type = "drawcard",
  prompt = "#rfenghou__mingqi",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 0,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = {}
    while not player.dead do
      local top = room:getNCards(1)[1]
      room:moveCardTo(top, Card.Processing, nil, fk.ReasonJustMove, self.name, nil, true, player.id)
      local bad = Fk:getCardById(top).type == Card.TypeEquip
      room:setCardEmotion(top, bad and "judgebad" or "judgegood")
      table.insert(cards, top)
      room:delay(600)
      if bad then
        break
      end
    end
    cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    if not player.dead and #cards > 0 then
      local targets = table.map(room.alive_players, Util.IdMapper)
      if #targets > 0 then
        local chosen = room:askForPoxi(player, "rfenghou__mingqi_choose", {
          { self.name, cards },
        }, {}, false)
        room:obtainCard(player, chosen, true, fk.ReasonJustMove, player.id, self.name)
        local num = math.min(#chosen, #player:getCardIds("he"))
        if num > 0 then
          local tos, giveCards = room:askForChooseCardsAndPlayers(player, num, num, table.map(room:getOtherPlayers(player, false),
          Util.IdMapper), 0, 1, ".", "#rfenghou__mingqi-give:::"..num, self.name, false)
          if #tos > 0 then
            room:obtainCard(room:getPlayerById(tos[1]), giveCards, true, fk.ReasonGive, player.id, self.name)
          else
            room:throwCard(giveCards, self.name, player, player)
          end
        end
      end
    end
    cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    if #cards > 0 then
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonJudge)
    end
  end,
}

local rfenghou__mingqi_trigger = fk.CreateTriggerSkill{
  name = "#rfenghou__mingqi_trigger",
  refresh_events = {fk.AfterDrawPileShuffle},
  can_refresh = function(self, event, target, player, data)
    local e = player.room.logic:getCurrentEvent():findParent(GameEvent.SkillEffect)
    return e and e.data[3] == rfenghou__mingqi and e.data[2] == player
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room.logic:getCurrentEvent():findParent(GameEvent.SkillEffect):addExitFunc(function()
      if not player.dead then
        room:killPlayer{who = player.id}
      end
    end)
  end,
}
rfenghou__mingqi:addRelatedSkill(rfenghou__mingqi_trigger)

lie:addSkill(rfenghou__mingqi)

Fk:addPoxiMethod{
  name = "rfenghou__mingqi_choose",
  card_filter = function(to_select, selected, data, extra_data)
    if data == nil or extra_data == nil then return false end
    if table.find(selected, function (id)
      return Fk:getCardById(id).trueName == Fk:getCardById(to_select).trueName
    end) then return false end
    local count = Fk:translate(Fk:getCardById(to_select).trueName, "zh_CN"):len()
    for _, id in ipairs(selected) do
      count = count + Fk:translate(Fk:getCardById(id).trueName, "zh_CN"):len()
    end
    return count <= 10
  end,
  feasible = function(selected, data, extra_data)
    return #selected > 0
  end,
  prompt = function ()
    return "#rfenghou__mingqi-card"
  end,
  default_choice = function (data, extra_data)
    return data and {data[1][2][1]} or {}
  end,
}


local rfenghou__huangzhu = fk.CreateTriggerSkill{
  name = "rfenghou__huangzhu",
  events = {fk.Death},
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self, false, true) and target == player
  end,
  on_cost = function(self, event, target, player, data)
    local tos = player.room:askForChoosePlayers(player, table.map(player.room.alive_players, Util.IdMapper), 1, 1, "#rfenghou__huangzhu-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    local ids, types = {}, {}
    local piles = table.connect(room.draw_pile, room.discard_pile)
    while #piles > 0 do
      local id = table.remove(piles, math.random(#piles))
      if Fk:getCardById(id).type == Card.TypeEquip and table.insertIfNeed(types, Fk:getCardById(id).sub_type) then
        table.insertIfNeed(ids, id)
      end
    end
    if #ids > 0 then
      room:moveCardTo(ids, Player.Hand, to, fk.ReasonJustMove, self.name)
    end
  end,
}
lie:addSkill(rfenghou__huangzhu)

Fk:loadTranslationTable{
  ["rfenghou__lie"] = "李娥",
  ["#rfenghou__lie"] = "炉尸传说",
  ["illustrator:rfenghou__lie"] = "",
  ["designer:rfenghou__lie"] = "会乱武的袁绍",

  ["rfenghou__mingqi"] = "命祈",
  [":rfenghou__mingqi"] = "出牌阶段限一次，你可以亮出牌堆顶的牌至亮出装备牌，然后获得亮出牌中任意张名称不同且名称字数之和不大于10的牌并选择：1.交给一名角色等量张牌；2.弃置等量张牌；若你以此法使得牌堆洗切，你于结算完成后死亡。",
  ["#rfenghou__mingqi"] = "命祈：你可以亮出牌堆顶牌直到亮出装备牌",
  ["#rfenghou__mingqi-card"] = "选择牌名不同且牌名字数之和不大于10的牌",
  ["rfenghou__mingqi_choose"] = "命祈 选牌",
  ["#rfenghou__mingqi_trigger"] = "命祈",
  ["#rfenghou__mingqi-give"] = "命祈：你须选择 %arg 张牌交给一名其他角色，不选目标则弃置之",

  ["rfenghou__huangzhu"] = "衁铸",
  [":rfenghou__huangzhu"] = "当你死亡时，你可以令一名角色从牌堆获得所有副类别的装备各一张。",
  ["#rfenghou__huangzhu-choose"] = "衁铸：请选择一名角色，令其获得所有副类别的装备各一张",
}

local shiyi = General:new(extension, "rfenghou__shiyi", "wu", 3)
local zoujian = fk.CreateTriggerSkill{
  name = "rfenghou__zoujian",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target ~= player and target.phase == Player.Discard and not target.dead and
      player:getHandcardNum() > 1
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askForDiscard(player, 2, 2, false, self.name, true, nil,
      "#rfenghou__zoujian::"..target.id, true)
    if #cards == 2 then
      self.cost_data = {tos = {target.id}, cards = cards}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = self.cost_data.cards
    local n = math.abs(Fk:getCardById(cards[1]).number - Fk:getCardById(cards[2]).number)
    local orig = target:getMaxCards()
    room:throwCard(cards, self.name, player, player)
    if not player.dead then
      player:drawCards(1, self.name)
    end
    if not target.dead then
      room:setPlayerMark(target, "@rfenghou__zoujian-round", tostring(n))
      if n < orig and target:isWounded() then
        room:recover{
          who = target,
          num = 1,
          recoverBy = player,
          skillName = self.name,
        }
      end
    end
  end,
}
local zoujian_maxcards = fk.CreateMaxCardsSkill{
  name = "#rfenghou__zoujian_maxcards",
  fixed_func = function(self, player)
    if player:getMark("@rfenghou__zoujian-round") ~= 0 then
      return tonumber(player:getMark("@rfenghou__zoujian-round"))
    end
  end
}
local gongzhui = fk.CreateTriggerSkill{
  name = "rfenghou__gongzhui",
  anim_type = "support",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player:isKongcheng() and player:usedSkillTimes(self.name, Player.HistoryRound) == 0 then
      local ret = false
      for _, move in ipairs(data) do
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              ret = true
              break
            end
          end
        end
      end
      if ret then
        return table.find(player.room.alive_players, function(p)
          return p:getHandcardNum() < math.min(p:getMaxCards(), 5)
        end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p)
      return p:getHandcardNum() < math.min(p:getMaxCards(), 5)
    end)
    local n = player.hp == 1 and 2 or 1
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, n,
      "#rfenghou__gongzhui-choose:::"..n, self.name, true)
    if #tos > 0 then
      room:sortPlayersByAction(tos)
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(self.cost_data.tos) do
      local p = room:getPlayerById(id)
      if not p.dead and p:getHandcardNum() < math.min(p:getMaxCards(), 5) then
        p:drawCards(math.min(p:getMaxCards(), 5) - p:getHandcardNum(), self.name)
      end
    end
  end,
}
zoujian:addRelatedSkill(zoujian_maxcards)
shiyi:addSkill(zoujian)
shiyi:addSkill(gongzhui)
Fk:loadTranslationTable{
  ["rfenghou__shiyi"] = "是仪",
  ["#rfenghou__shiyi"] = "冠玉未瑕",
  ["illustrator:rfenghou__shiyi"] = "凡果",
  ["designer:rfenghou__shiyi"] = "一网打尽岁月",

  ["rfenghou__zoujian"] = "诹谏",
  [":rfenghou__zoujian"] = "一名其他角色的弃牌阶段开始时，你可以弃置两张手牌并摸一张牌，然后将其此轮手牌上限改为这两张牌点数之差。"..
  "若因此减少，其回复1点体力。",
  ["rfenghou__gongzhui"] = "恭追",
  [":rfenghou__gongzhui"] = "每轮限一次，当你失去最后的手牌后，你可以令一名角色将手牌补至手牌上限（至多为五），若你体力值为1，可以额外选择"..
  "一名角色。",
  ["#rfenghou__zoujian"] = "诹谏：你可弃2张手牌并摸1张牌，将 %dest 本轮手牌上限改为这两牌点数之差",
  ["@rfenghou__zoujian-round"] = "诹谏",
  ["#rfenghou__gongzhui-choose"] = "恭追：你可以令%arg名角色将手牌补至手牌上限（至多为五）",

  ["$rfenghou__zoujian1"] = "臣所言者皆为所知者，辞不敢移。",
  ["$rfenghou__zoujian2"] = "少主有至尊之风，可尽心佐之。",
  ["$rfenghou__gongzhui1"] = "不羡秋月春风，只擎殿陛砖瓦。",
  ["$rfenghou__gongzhui2"] = "此身长于草木，当为国之柱石。",
  ["~rfenghou__shiyi"] = "欲从真龙，奈何年迈。",
}

local dongfeng = General:new(extension, "rfenghou__dongfeng", "wu", 3)

local rfenghou__ganjin = fk.CreateActiveSkill{
  name = "rfenghou__ganjin",
  anim_type = "switch",
  switch_skill_name = "rfenghou__ganjin",
  card_num = 0,
  target_num = 1,
  card_filter = Util.FalseFunc,
  prompt = function (self)
    local effect = Self:getSwitchSkillState(self.name) == fk.SwitchYang and "rfenghou__ganjin_recover" or "rfenghou__ganjin_draw"
    return "#rfenghou__ganjin:::"..effect
  end,
  target_filter = function(self, to_select, selected)
    if #selected > 0 then return false end
    local to = Fk:currentRoom():getPlayerById(to_select)
    if Self:getSwitchSkillState(self.name) == fk.SwitchYang and not to:isWounded() then return false end
    return true
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    if player:getSwitchSkillState(self.name, true) == fk.SwitchYang then
      room:recover { num = 1, skillName = self.name, who = to, recoverBy = player }
    else
      to:drawCards(2, self.name)
    end
    if to.dead or to:isNude() then return end
    local _, dat = room:askForUseActiveSkill(to, "rfenghou__ganjin_other", "#rfenghou__ganjin-other")
    if not dat then return end
    local choice, cards = dat.interaction, dat.cards
    if choice == "rfenghou__ganjin_use" then
      room:useCard{ card = Fk:getCardById(cards[1]), from = to.id, tos = {{to.id}} }
    else
      room:recastCard(cards, to, self.name)
    end
    player:setSkillUseHistory(self.name, 0, Player.HistoryPhase)
  end,
}

local rfenghou__ganjin_other = fk.CreateActiveSkill{
  name = "rfenghou__ganjin_other",
  anim_type = "offensive",
  card_num = 1,
  target_num = 0,
  interaction = function()
    return UI.ComboBox { choices = {"rfenghou__ganjin_use", "rfenghou__ganjin_recast"} }
  end,
  card_filter = function(self, to_select, selected)
    local card = Fk:getCardById(to_select)
    if #selected > 0 or card.color ~= Card.Red then return false end
    if card.type == Card.TypeEquip or card.is_passive or (not card.multiple_targets and card.skill:getMinTargetNum() == 0) then
      if self.interaction.data == "rfenghou__ganjin_recast" then return true end
      if table.contains(Self:getCardIds("e"), to_select) or Self:prohibitUse(card) or Self:isProhibited(Self, card) then return false end
      return Self:canUse(card)
    end
  end,
}
Fk:addSkill(rfenghou__ganjin_other)

local rfenghou__ganjin_record = fk.CreateTriggerSkill{
  name = "#rfenghou__ganjin_record",

  refresh_events = {fk.EnterDying, fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.EnterDying then
      return target == player and player:getMark("rfenghou__ganjin_dying") == 0
    else
      return target == player and data == rfenghou__ganjin
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EnterDying then
      room:setPlayerMark(player, "rfenghou__ganjin_dying", 1)
    else
      room.logic:getEventsOfScope(GameEvent.Dying, 1, function(e)
        local who = room:getPlayerById(e.data[1].who)
        if who and who:getMark("rfenghou__ganjin_dying") == 0 then
          room:setPlayerMark(who, "rfenghou__ganjin_dying", 1)
        end
      end, Player.HistoryGame)
    end
  end,
}
rfenghou__ganjin:addRelatedSkill(rfenghou__ganjin_record)

dongfeng:addSkill(rfenghou__ganjin)

local rfenghou__zhuilu = fk.CreateTriggerSkill{
  name = "rfenghou__zhuilu",
  events = {fk.EnterDying},
  frequency = Skill.Limited,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target == player and player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#rfenghou__zhuilu-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = { tos = tos }
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    room:handleAddLoseSkills(to, "rfenghou__shengxi|rfenghou__quji")
  end,
}
dongfeng:addSkill(rfenghou__zhuilu)

local shengxi = fk.CreateTriggerSkill{
  name = "rfenghou__shengxi",
  anim_type = "drawcard",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player == target and player:hasSkill(self) and player.phase == Player.Finish and
    #player.room.logic:getActualDamageEvents(1, function(e) return e.data[1].from == player end) == 0
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(2, self.name)
  end,
}
dongfeng:addRelatedSkill(shengxi)

local quji = fk.CreateActiveSkill{
  name = "rfenghou__quji",
  anim_type = "support",
  min_card_num = function ()
    return Self:getLostHp()
  end,
  min_target_num = 1,
  prompt = function ()
    return "#rfenghou__quji:::"..Self:getLostHp()
  end,
  can_use = function(self, player)
    return player:isWounded() and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function(self, to_select, selected)
    return #selected < Self:getLostHp()
  end,
  target_filter = function(self, to_select, selected, cards)
    return #selected < Self:getLostHp() and Fk:currentRoom():getPlayerById(to_select):isWounded()
    and #cards == Self:getLostHp()
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local loseHp = table.find(effect.cards, function(id) return Fk:getCardById(id).color == Card.Black end)
    room:throwCard(effect.cards, self.name, player, player)
    local tos = effect.tos
    room:sortPlayersByAction(tos)
    for _, pid in ipairs(tos) do
      local to = room:getPlayerById(pid)
      if not to.dead and to:isWounded() then
        room:recover({
          who = to,
          num = 1,
          recoverBy = player,
          skillName = self.name
        })
      end
    end
    if loseHp and not player.dead then
      room:loseHp(player, 1, self.name)
    end
  end,
}
dongfeng:addRelatedSkill(quji)

Fk:loadTranslationTable{
  ["rfenghou__dongfeng"] = "董奉",
  ["#rfenghou__dongfeng"] = "",
  ["illustrator:rfenghou__dongfeng"] = "",
  ["designer:rfenghou__dongfeng"] = "会乱武的袁绍",

  ["rfenghou__ganjin"] = "甘津",
  [":rfenghou__ganjin"] = "转换技，出牌阶段限一次，你可令一名角色①回复1点体力②摸两张牌，然后该角色可使用或重铸一张不能指定其他角色为目标的"..
  "红色牌以令〖甘津〗视为未发动。",
  ["#rfenghou__ganjin"] = "甘津：令一名角色：%arg",
  ["rfenghou__ganjin_recover"] = "回复1点体力",
  ["rfenghou__ganjin_draw"] = "摸2张牌",
  ["#rfenghou__ganjin-other"] = "你可以使用或重铸一张不能指定其他角色为目标的红色牌以重置“甘津”",
  ["rfenghou__ganjin_use"] = "使用",
  ["rfenghou__ganjin_recast"] = "重铸",
  ["rfenghou__ganjin_other"] = "甘津",

  ["rfenghou__zhuilu"] = "坠露",
  [":rfenghou__zhuilu"] = "限定技，你进入濒死时，可以令一名角色获得“生息”、“去疾”。",
  ["#rfenghou__zhuilu-choose"] = "坠露：你可以令一名角色获得“生息”、“去疾”",

  ["rfenghou__quji"] = "去疾",
  [":rfenghou__quji"] = "出牌阶段限一次，你可以弃置X张牌并选择至多X名已受伤的角色，令这些角色各回复1点体力，然后若你以此法弃置过的牌中有黑色牌，你失去1点体力。（X为你已损失的体力值）",
  ["#rfenghou__quji"] = "去疾：弃置%arg张牌，令至多等量角色回复体力，若弃了黑色牌，你失去1点体力",

  ["rfenghou__shengxi"] = "生息",
  [":rfenghou__shengxi"] = "结束阶段，若你于此回合内未造成过伤害，你可摸两张牌。",

  ["$rfenghou__zhuilu1"] = "",
  ["$rfenghou__zhuilu2"] = "",
}

return extension
